package com.kuang.springclound.controller;

import com.kuang.springclound.pojo.SystemDept;
import com.kuang.springclound.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//提供Restfull服务
@RestController
public class DeptController {

    @Resource
    DeptService deptService;

    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixQueryById")
    public SystemDept queryById(@PathVariable("id") Long id) {
        SystemDept systemDept = deptService.queryById(id);

        if (systemDept==null){
            throw new RuntimeException("id=>"+id+"不存在该用户，或者信息无法查到");
        }

        return systemDept;
    }

    //备选方案
    public SystemDept hystrixQueryById(@PathVariable("id") Long id) {
        return new SystemDept()
                .setDept_id(id)
                .setDept_name("id=>"+id+"没有对应的信息，null--@Hystrix")
                .setDb_source("no this database in MySQL");
    }

}

